/// <reference path="typings/index.d.ts" />
'use strict';
const path = require('path');
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const run = require('run-sequence');
const rimraf = require('rimraf');
const glob = require('glob');

const bsync = require('browser-sync');
const reload = bsync.reload;

const app = {
    basePath: require('./bower.json').appPath || 'app',
    dist: './dist'
};

const paths = {
    scripts: [app.basePath + '/scripts/**/*.js'],
    styles: [app.basePath + '/styles/**/*.scss'],
    jsRequire: [
        app.basePath + '/bower_components/angular/angular.js',
        app.basePath + '/bower_components/angular-loader/angular-loader.js',
        app.basePath + '/bower_components/angular-mocks/angular-mocks.js',
        app.basePath + '/bower_components/angular-route/angular-route.js',
        app.basePath + '/bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js',
        app.basePath + '/bower_components/jquery/dist/jquery.min.js',
        app.basePath + '/bower_components/bootstrap/dist/js/bootstrap.min.js',
        app.basePath + '/bower_components/requirejs/require.js',
    ],
    cssRequire: [
        app.basePath + '/bower_components/bootstrap/dist/css/bootstrap.css',
        app.basePath + '/bower_components/html5-boilerplate/dist/css/main.css',
        app.basePath + '/bower_components/html5-boilerplate/dist/css/normalize.css'
    ],
};

gulp.task('clear', callback => {
    console.log('this is clear task............................');
    rimraf(app.dist + '/**/*', callback);
});

/**sass */
gulp.task('sass', () => {
    console.log('this is sass task..................');
    gulp.src(paths.styles)
        .pipe($.plumber())
        .pipe($.sass({
            // outputStyle: 'expanded'
            outputStyle: 'compressed'
        }))
        .pipe(gulp.dest('./app/styles/'))
        .pipe(reload({
            stream: true
        }));
});

gulp.task('watch:css', () => {
    gulp.watch(paths.styles, ['sass']);
});
gulp.task('watch:js', () => {
    gulp.watch('app/view/**/*.js', reload);
});
gulp.task('watch:html', () => {
    // gulp.watch([app.basePath + '/**/*.js', app.basePath + '/**/*.html'], ['copy']);
    gulp.watch([app.basePath + '/view/**/*.html'], reload);
});

/** */
gulp.task('watch:roothtml', () => {
    gulp.watch('./app/index.html', reload);
});
/** */
gulp.task('watch:rootjs', () => {
    gulp.watch('./app/*.js', reload);
});
/** */

// 静态服务器
gulp.task('bserver', () => {
    bsync.init({
        server: {
            baseDir: "./app"
        }
    });
});
gulp.task('watch', cb => {
    // content
    run(['watch:css',
        // 'watch:js',
        'watch:html',
        'watch:roothtml',
        // 'watch:rootjs'
    ], cb);
});
gulp.task('default', () => {
    run(['sass'], [
        'bserver',
        'watch'
    ]);
    console.log('this is default task!!!');
});